<template>
  <div class="query-input-box">
    <input v-model="query" type="text"
           @blur="blurQueryEvent" @keyup.enter="enterQueryEvent"
           placeholder="搜索" class="input-text"/>
    <span class="input-icon"><i class="fa fa-search" aria-hidden="true"></i></span>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const query = ref<string>('')

const enterQueryEvent = () => {
  console.log(query.value)
}

const blurQueryEvent = () => {
  query.value = ''
}
</script>

<style scoped lang="scss">
$theme-color: #009FA8;
$query-input-width: 250px;

.query-input-box {
  position: relative;
  display: inline-block;

  .input-text {
    padding: 0 0 0 40px;
    width: 120px;
    height: 40px;
    font-size: 16px;
    border: 1px solid $theme-color;
    border-radius: 15px;
    background: #FFFFFF;
    transition: width .5s;
    transition-delay: .1s;
  }

  .input-text:focus {
    width: $query-input-width;
    outline: none;
    box-shadow: none;
  }

  .input-text:focus+.span {
    background-color: $theme-color;
    color: #fff;
  }

  .input-icon {
    position: absolute;
    top: 5px;
    left: 5px;
    width: 30px;
    height: 30px;
    line-height: 30px;
    padding: 0;
    text-align: center;
    background: #F5F5F5;
    border-radius: 50%;
    font-size: 15px;
    cursor: pointer;
  }
}
</style>
